package redis

import (
	"FeedXmh/app/model"
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	"strings"
)

// GetGwAlive 获取终端在线状态
func GetGwAlive(device_addr string) (alive bool) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:alive", model.Redis_Node_Prefix, device_addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			//
			alive = false
		} else {
			if gconv.Int(gconv.String(val)) == 1 {
				alive = true
			} else {
				alive = false
			}
		}
	}
	return alive
}

// SetGwAlive 设置终端在线状态
func SetGwAlive(device_addr string, alive bool) {
	val := "0"
	if alive {
		val = "1"
	}
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:alive", model.Redis_Node_Prefix, device_addr), val)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:alive=%s", model.Redis_Node_Prefix, device_addr, val)
	}
}

// GetGwNodeAlive 获取饲喂器在线状态
func GetGwNodeAlive(device_addr string, sn string) (alive bool) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:online", model.Redis_Node_Prefix, device_addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			//
			alive = false
		} else {
			if gconv.Int(gconv.String(val)) == 1 {
				alive = true
			} else {
				alive = false
			}
		}
	}
	return alive
}

// GetGwNodeAliveVal GetGwNodeAlive 获取饲喂器在线状态
func GetGwNodeAliveVal(device_addr string, sn string) (val interface{}) {
	val, _ = g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:online", model.Redis_Node_Prefix, device_addr, sn))
	return val
}

// SetGwNodeAlive 设置饲喂器在线状态
func SetGwNodeAlive(device_addr string, sn string, alive bool) {
	val := "0"
	if alive {
		val = "1"
	}
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:online", model.Redis_Node_Prefix, device_addr, sn), val)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:online=%s", model.Redis_Node_Prefix, device_addr, sn, val)
	}
}

// SetGwNodeAliveVal 设置饲喂器在线状态
func SetGwNodeAliveVal(device_addr string, sn string, val string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:online", model.Redis_Node_Prefix, device_addr, sn), val)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:online=%s", model.Redis_Node_Prefix, device_addr, sn, val)
	}
}

// GetGwNodeFirstOnline 获取饲喂器第一次在线时间
func GetGwNodeFirstOnline(addr string, sn string) (exist bool, value string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:first_online", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			//
			exist = false
		} else {
			exist = true
			value = gconv.String(val)
		}
	}
	return exist, value
}

// SetGwNodeFirstOnline 设置饲喂器在线状态
func SetGwNodeFirstOnline(addr string, sn string, first_online string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:first_online", model.Redis_Node_Prefix, addr, sn), first_online)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:first_online=%s", model.Redis_Node_Prefix, addr, sn, first_online)
	}
}

// GetGwNodeAlarm 获取饲喂器告警状态
func GetGwNodeAlarm(addr string, sn string) (alarm string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:alarm", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			alarm = ""
		} else {
			alarm = gconv.String(val)
		}
	}
	return alarm
}

// SetGwNodeAlarm 设置饲喂器告警状态
func SetGwNodeAlarm(addr string, sn string, alarm string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:alarm", model.Redis_Node_Prefix, addr, sn), alarm)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:alarm=%s", model.Redis_Node_Prefix, addr, sn, alarm)
	}
}

// GetGwNodeCrc16 获取饲喂器Crc16状态
func GetGwNodeCrc16(addr string, sn string) (crc16 string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:crc16", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			crc16 = ""
		} else {
			crc16 = gconv.String(val)
		}
	}
	return crc16
}

// SetGwNodeCrc16 设置饲喂器Crc16状态
func SetGwNodeCrc16(addr string, sn string, crc16 string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:crc16", model.Redis_Node_Prefix, addr, sn), crc16)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:crc16=%s", model.Redis_Node_Prefix, addr, sn, crc16)
	}
}

// GetGwNodePreDayFeed 获取饲喂器昨日下料/目标
//
//	pre_day_feed 格式:feed/max_feed
func GetGwNodePreDayFeed(addr string, sn string) (pre_day_feed string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:pre_day_feed", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			pre_day_feed = "0/0"
		} else {
			pre_day_feed = gconv.String(val)
		}
	}
	return pre_day_feed
}

// SetGwNodePreDayFeed 设置饲喂器昨日下料/目标
//
//	pre_day_feed 格式:feed/max_feed
func SetGwNodePreDayFeed(addr string, sn string, pre_day_feed string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:pre_day_feed", model.Redis_Node_Prefix, addr, sn), pre_day_feed)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:pre_day_feed=%s", model.Redis_Node_Prefix, addr, sn, pre_day_feed)
	}
}

// GetGwNodeStatus
func GetGwNodeStatus(addr string, sn string) (status_json string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:status_json", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			status_json = ""
		} else {
			status_json = gconv.String(val)
		}
	}
	return status_json
}

// SetGwNodeStatus
func SetGwNodeStatus(addr string, sn string, status_json string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:status_json", model.Redis_Node_Prefix, addr, sn), status_json)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:status_json=%s", model.Redis_Node_Prefix, addr, sn, status_json)
	}
}

// GetGwLastRid 获取饲喂器LastRid状态
func GetGwLastRid(addr string) (last_rid string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:last_rid", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			last_rid = ""
		} else {
			last_rid = gconv.String(val)
		}
	}
	return last_rid
}

// SetGwLastRid 设置饲喂器LastRid状态
func SetGwLastRid(addr string, last_rid string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:last_rid", model.Redis_Node_Prefix, addr), last_rid)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:last_rid=%s", model.Redis_Node_Prefix, addr, last_rid)
	}
}

// GetGwMinMaxRid 获取饲喂器minid maxid状态
func GetGwMinMaxRid(addr string) (minid string, maxid string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:minid_maxid", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			minid = ""
			maxid = ""
		} else {
			minid_maxid := gconv.String(val)
			ids := strings.Split(minid_maxid, "_")
			if len(ids) == 2 {
				minid = ids[0]
				maxid = ids[1]
			}
		}
	}
	return minid, maxid
}

// SetGwMinMaxRid 设置饲喂器LastRid状态
func SetGwMinMaxRid(addr string, minid uint64, maxid uint64) {
	ids := fmt.Sprintf("%d_%d", minid, maxid)
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:minid_maxid", model.Redis_Node_Prefix, addr), ids)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:minid_maxid=%s", model.Redis_Node_Prefix, addr, ids)
	}
}

// GetGwAllNodeVersion 获取终端内饲喂器版本
func GetGwAllNodeVersion(addr string) (node_version string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node_version", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			node_version = ""
		} else {
			node_version = gconv.String(val)
		}
	}
	return node_version
}

// SetGwAllNodeVersion 设置终端内饲喂器版本
func SetGwAllNodeVersion(addr string, node_version string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node_version", model.Redis_Node_Prefix, addr), node_version)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node_version=%s", model.Redis_Node_Prefix, addr, node_version)
	}
}

func GetGwNodeNum(addr string) (node_num string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node_num", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			node_num = "0"
		} else {
			node_num = gconv.String(val)
		}
	}
	return node_num
}

func SetGwNodeNum(addr string, node_num string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node_num", model.Redis_Node_Prefix, addr), node_num)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node_num=%s", model.Redis_Node_Prefix, addr, node_num)
	}
}

func SetGwonlineNodeNum(addr string, online_node_num string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:online_node_num", model.Redis_Node_Prefix, addr), online_node_num)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:online_node_num=%s", model.Redis_Node_Prefix, addr, online_node_num)
	}
}
func GetGwOnlineNodeNum(addr string) (online_node_num string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:online_node_num", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			online_node_num = "0"
		} else {
			online_node_num = gconv.String(val)
		}
	}
	return online_node_num
}

func GetGwBusyFlag(addr string) (busy_flag int) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:busy_flag", model.Redis_Node_Prefix, addr))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			busy_flag = 0
		} else {
			busy_flag = gconv.Int(gconv.String(val))
		}
	}
	return busy_flag
}

func SetGwBusyFlag(addr string, busy_flag int) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:busy_flag", model.Redis_Node_Prefix, addr), gconv.String(busy_flag))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:busy_flag=%d", model.Redis_Node_Prefix, addr, busy_flag)
	}
}

// GetGwNodePigNum
func GetGwNodePigNum(addr string, sn string) (pig_num string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:pig_num", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			pig_num = ""
		} else {
			pig_num = gconv.String(val)
		}
	}
	return pig_num
}

// SetGwNodePigNum
func SetGwNodePigNum(addr string, sn string, pig_num string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:pig_num", model.Redis_Node_Prefix, addr, sn), pig_num)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:pig_num=%s", model.Redis_Node_Prefix, addr, sn, pig_num)
	}
}

// GetGwNodeSonNum
func GetGwNodeSonNum(addr string, sn string) (son_num string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:son_num", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			son_num = ""
		} else {
			son_num = gconv.String(val)
		}
	}
	return son_num
}

// SetGwNodeSonNum
func SetGwNodeSonNum(addr string, sn string, son_num string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:son_num", model.Redis_Node_Prefix, addr, sn), son_num)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:son_num=%s", model.Redis_Node_Prefix, addr, sn, son_num)
	}
}

// GetGwNodeHnum 获取饲喂器栏位号
func GetGwNodeHnum(addr string, sn string) (hnum string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:hnum", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			hnum = ""
		} else {
			hnum = gconv.String(val)
		}
	}
	return hnum
}

// SetGwNodeHnum 设置饲喂器栏位号
func SetGwNodeHnum(addr string, sn string, hnum string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:hnum", model.Redis_Node_Prefix, addr, sn), hnum)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:hnum=%s", model.Redis_Node_Prefix, addr, sn, hnum)
	}
}

// GetGwNodeBakupPara 获取饲喂器备份参数
func GetGwNodeBakupPara(addr string, sn string) (backup_para string) {
	val, err := g.Redis("default").Do("get", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:backup_para", model.Redis_Node_Prefix, addr, sn))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		if val == nil {
			backup_para = ""
		} else {
			backup_para = gconv.String(val)
		}
	}
	return backup_para
}

// SetGwNodeBakupPara 设置饲喂器备份参数
func SetGwNodeBakupPara(addr string, sn string, backup_para string) {
	_, err := g.Redis("default").Do("set", fmt.Sprintf("%s:device:DMXFGW:%s:node:%s:backup_para", model.Redis_Node_Prefix, addr, sn), backup_para)
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debugf("Redis set %s:device:DMXFGW:%s:node:%s:backup_para=%s", model.Redis_Node_Prefix, addr, sn, backup_para)
	}
}
